<!DOCTYPE html>
<html lang='en'>

<head>
    <meta charset='UTF-8'>
    <meta name='viewport' content='width=device-width, initial-scale=1.0'>
    <title>Document</title>
</head>

<body>
    <div id="app">
        <div v-for="(item,index) of arr" :key='index'>
            <input type="checkbox" v-model="item.isChecked">
            <span>{{index + 1}}--</span>
            <span>{{item.name}}--</span>
            <span>{{item.price | money}}</span>
            <span>
                <button v-on:click="item.num<=0 ? 0 : item.num -= 1">-</button>
                <input type="text" style="width:20px" v-model="item.num"
                    v-on:keyup="item.num=item.num <= 0 ? 0 : item.num">
                <button v-on:click="item.num += 1">+</button>
            </span>
            <span>--小计:{{item.num * item.price }}</span>
        </div>
        <span>
            <span>
                总价:{{total }}
            </span>
        </span>
    </div>
    </div>

    <script src="./js/vue.js"></script>

    <script type="text/javascript">
        var vm = new Vue({
            el: '#app',
            data: {
                arr: [{
                        name: '帽子',
                        price: 10,
                        num: 1,
                        isChecked: false
                    },
                    {
                        name: '裤子',
                        price: 20,
                        num: 1,
                        isChecked: false
                    },
                    {
                        name: '鞋子',
                        price: 30,
                        num: 1,
                        isChecked: false
                    },
                ]
            },
            computed: {
                total: function () {
                    var totalPrice = 0;
                    this.arr.forEach(el => {
                        if (el.isChecked == true) {
                            totalPrice += el.price * el.num;
                        }
                        this.totalPrice = totalPrice;
                    });
                    return totalPrice;
                }
            },
        })
    </script>
</body>

</html>